package com.decoder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.decoder.bean.Addivieinfo;
import com.decoder.util.ILoadDataResponse;
import com.decoder.util.InformationDataLoader;

public class FoodSafeInfomationActivity extends Activity {
	
	private static final int UPDATE_LISTVIEW = 1025;
	private static final int ERROR = 1026;
	
	private ListView informationListView;
	private View loadingView;
	private List<Addivieinfo> infos = new ArrayList<Addivieinfo>();
	private InformationListViewAdapter adapter;
	
	private Activity mActivity;
	InformationDataLoader dataLoader;
	ILoadDataResponse dataResponse = new ILoadDataResponse(){

		@Override
		public void onLoadDataComplete(ArrayList<Addivieinfo> nextInfo,
				int total, int current) {
			infos.addAll(nextInfo);
			Message msg = mHandler.obtainMessage();
			msg.what = UPDATE_LISTVIEW;
			msg.obj = current + "/" + total + "songs";
			msg.sendToTarget();
		}

		@Override
		public void onLoadDataError(String errorMsg) {
			Message msg = mHandler.obtainMessage();
			msg.what = ERROR;
			msg.obj = errorMsg;
			msg.sendToTarget();
		}
		
	};
	
	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch(msg.what) {
			case UPDATE_LISTVIEW:
				adapter.notifyDataSetChanged();
				informationListView.removeFooterView(loadingView);
				break;
			case ERROR:
				Toast.makeText(mActivity, (String) msg.obj, Toast.LENGTH_SHORT).show();
				informationListView.removeFooterView(loadingView);
				break;
			}
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mActivity = this;
		dataLoader = new InformationDataLoader(dataResponse);
		initUI();
	}
	
	private void initUI() {
		setContentView(R.layout.foodsafeinformation);
		LayoutInflater inflater = LayoutInflater.from(mActivity);
		loadingView = inflater.inflate(R.layout.loading, null);
		informationListView = (ListView) findViewById(R.id.informationListView);
		adapter = new InformationListViewAdapter();
		informationListView.setOnScrollListener(new MyScrollListener());
		informationListView.setAdapter(adapter);
	}
	
	private void loadNext(){
		informationListView.addFooterView(loadingView);
		dataLoader.loadNext();
	}
	
	class MyScrollListener implements OnScrollListener {

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			if(firstVisibleItem + visibleItemCount < totalItemCount - 3)
				return;
			if(dataLoader.isLoading() == false){
				loadNext();
			}
		}

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {

		}
	}
	
	class ViewHolder {
		TextView tv_title;
		TextView tv_content;
		TextView tv_date;
	}
	
	private class InformationListViewAdapter extends BaseAdapter {
		
		LayoutInflater inflater = null;

		@Override
		public int getCount() {
			return infos.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if(convertView == null) {
				if (inflater == null) {
					inflater = (LayoutInflater) mActivity
							.getSystemService(LAYOUT_INFLATER_SERVICE);
				}
				convertView = inflater.inflate(R.layout.item_normal, null);
				viewHolder = new ViewHolder();
				viewHolder.tv_title = (TextView) convertView.findViewById(R.id.informationTitle);
				viewHolder.tv_content = (TextView) convertView.findViewById(R.id.informationContent);
				viewHolder.tv_date = (TextView) convertView.findViewById(R.id.informationDate);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			viewHolder.tv_title.setText(infos.get(position).getAddivieInfoTitle());
			viewHolder.tv_content.setText(infos.get(position).getAddivieInfoContent());
			Date date = infos.get(position).getInfoPublishTime();
			viewHolder.tv_date.setText(date.getHours() + ":" + date.getMinutes() + "");
			return convertView;
		}
		
	}

}
